# -*- coding:utf-8 -*-
# @File    : writeCase.py
# ************************
import os,sys
from config import ROOT_DIR,PAGE_DIR
from comm.script.writeCaseYml import read_yaml_data, txt_change_yaml

temp_file = ROOT_DIR+'config/test_template.py'


def write_case(case_path, auto_yaml=False,auto_py=False):
    """

    :param case_path: 用例路径，当auto_yaml为True时，需要传入data目录，否则传入扫描目录
    :param auto_yaml: 是否自动生成yaml文件
    :return:
    """
    # 判断是否自动生成yaml用例
    if auto_yaml:
        yaml_list = txt_change_yaml(case_path)
    else:
        yaml_list = list()
        file_list = os.listdir(case_path)
        for file in file_list:
            if '.yaml' in file:
                yaml_path = case_path+'/'+file
                yaml_list.append(yaml_path)
            else:
                yaml_path = case_path + '/' + file
                yaml_file_list = os.listdir(yaml_path)
                for yaml_file in yaml_file_list:
                    y_path = yaml_path + '/' + yaml_file
                    yaml_list.append(y_path)
    if auto_py:
        return
    # 遍历测试用例列表
    for yaml_file in yaml_list:
        test_data = read_yaml_data(yaml_file)
        test_script = yaml_file.replace('\\', '/').replace('/page/', '/testcase/').replace('.yaml', '.py')
        case_name = os.path.basename(test_script).replace('.py', '')
        case_path = os.path.dirname(test_script)
        # 判断文件路径是否存在
        if not os.path.exists(case_path):
            os.makedirs(case_path)

        # 判断测试用例脚本是否存在
        if os.path.exists(test_script):
            continue

        # 替换模板内容
        file_data = ''
        with open(temp_file, "r", encoding="utf-8") as f:
            for line in f:
                if 'TestTemplate' in line:
                    title = test_data['test_info']['title']
                    line = line.replace('Template', title.title())
                    if '_' in line:
                        line = line.replace('_', '')
                if 'test_template' in line:
                    line = line.replace('test_template', case_name)
                file_data += line

        # 写入新脚本
        with open(test_script, "w", encoding="utf-8") as f:
            f.write(file_data)

def add_prefix_files(path):             #定义函数名称
    mark = 'test_mobile_'                 #准备添加的前缀内容
    for foldName, subfolders, filenames in os.walk(path):     #用os.walk方法取得path路径下的文件夹路径，子文件夹名，所有文件名
        for filename in filenames:     #遍历列表下的子文件夹名
            if filename!= sys.argv[0]:  #代码本身文件路径，防止脚本文件放在path路径下时，被一起重命名
               if filename.endswith('.py'):   #当文件名以.txt后缀结尾时
                   if mark not in filename:
                        os.rename(os.path.join(foldName,filename),os.path.join(foldName,str(filename).replace("test_",mark)))  #重命名文件
               if filename.endswith('.yaml'):  # 当文件名以.txt后缀结尾时
                   if mark not in filename:
                       os.rename(os.path.join(foldName, filename), os.path.join(foldName, str(filename).replace("test_",mark)))  # 重命名文件



# if __name__ == '__main__':
#     real_path = os.path.dirname(os.path.realpath(__file__)).replace('\\', '/')
#     write_case(real_path + '/data', auto_yaml=True)
    # write_case(real_path+'/page/oauth', auto_yaml=False)
